package top.aicey.ai.sparring.controller;

import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import top.aicey.ai.sparring.biz.ISceneBiz;
import top.aicey.ai.sparring.common.constants.ErrorConstants;
import top.aicey.ai.sparring.common.constants.QueryParamsConstants;
import top.aicey.ai.sparring.common.holder.CurrentUser;
import top.aicey.ai.sparring.common.holder.UserHolder;
import top.aicey.ai.sparring.common.utils.BaseResponseUtil;
import top.aicey.ai.sparring.dto.enums.SceneStatusEnum;
import top.aicey.ai.sparring.dto.model.BaseResponse;
import top.aicey.ai.sparring.dto.req.SaveSceneRequestDto;
import top.aicey.ai.sparring.dto.res.AdminSceneResponse;
import top.aicey.ai.sparring.dto.res.SceneDetailResponse;

/**
 * <p>
 * 企业场景信息 前端控制器
 * </p>
 *
 * @author 白杨
 * @since 2024-12-19
 */
@Api(tags = "AI陪练管理")
@RestController
@RequestMapping("/v3/sparing/trainingScene/admin")
@ApiImplicitParams({
        @ApiImplicitParam(name = "x-access-token",value = "token",required = true,paramType = "header")
})
@Slf4j
public class SceneAdminController {

    @Autowired
    private ISceneBiz sceneBiz;

    @ApiOperation(value = "场景列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",required = true),
            @ApiImplicitParam(name = "keyword",value = "搜索关键词",required = false),
    })
    @PostMapping(value = "/list")
    public BaseResponse<PageInfo<AdminSceneResponse>> list(
            @RequestParam(value = "pageNumber", defaultValue = "1", required = false) Integer pageNumber,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(value = "status", required = false) SceneStatusEnum status,
            @RequestParam(value = "keyword",  required = false) String keyword
    ){

        CurrentUser user = UserHolder.getUser();

        // 读取企业id
        String moduleId = user.getModuleId();
        Map<String,Object> params = Maps.newHashMap();
        params.put(QueryParamsConstants.STATUS,status);
        params.put(QueryParamsConstants.KEYWORD,keyword);

        PageInfo<AdminSceneResponse> page = sceneBiz.pageAdminQuery(moduleId,pageNumber,pageSize,params);

        return BaseResponseUtil.buildSuccessResponse(page);
    }

    /**
     * 删除
     * @param id id
     * @return
     */
    @ApiOperation(value = "删除")
    @GetMapping(value = "delete")
    public BaseResponse<Boolean> delete(@RequestParam("id") Long id){
        if (Objects.isNull(sceneBiz.exists(id))) {
            return BaseResponseUtil.buildFailedResponse(ErrorConstants.INVALID_PARAMS);
        }
        Boolean isRemoved = sceneBiz.removeById(id);
        return BaseResponseUtil.buildSuccessResponse(isRemoved);
    }

    /**
     * 保存场景信息
     * @param dto
     * @return
     */
    @ApiOperation(value = "保存场景信息")
    @PostMapping(value = "save")
    public BaseResponse<Long> save(@RequestBody SaveSceneRequestDto dto){

        if (Objects.isNull(dto)
                // 评估维度不能为空
                || CollectionUtils.isEmpty(dto.getDimensions())
                // 问答对不能为空
                || CollectionUtils.isEmpty(dto.getQaList())
                // 名字不能为空
                || StringUtils.hasText(dto.getName())
                // 图标不能为空
                || StringUtils.hasText(dto.getIcon())
                // 描述不能为空
                || StringUtils.hasText(dto.getSceneDescribe())

        ){
            return BaseResponseUtil.buildFailedResponse(ErrorConstants.INVALID_PARAMS);
        }

        // 开始校验
        CurrentUser user = UserHolder.getUser();
        String userId = user.getUserId();
        String moduleId = user.getModuleId();
        Long sceneId = sceneBiz.save(moduleId, userId, dto);
        return BaseResponseUtil.buildSuccessResponse(sceneId);
    }

    /**
     * 修改场景状态
     * @param id 场景id
     * @param statusEnum 场景状态
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "场景id",required = true),
            @ApiImplicitParam(name = "status",value = "状态",required = true,dataTypeClass = SceneStatusEnum.class),
    })
    @ApiOperation(value = "修改状态")
    @PostMapping(value = "status-change")
    public BaseResponse<Long> status(@RequestParam("id")Long sceneId,@RequestParam("status") SceneStatusEnum statusEnum){
        if (Objects.isNull(sceneBiz.exists(sceneId)) || Objects.isNull(statusEnum)){
            return BaseResponseUtil.buildFailedResponse(ErrorConstants.INVALID_PARAMS);
        }

        boolean isSuccess = sceneBiz.statusChange(sceneId, statusEnum,UserHolder.getUser().getModuleId(), UserHolder.getUser().getUserId());
        return BaseResponseUtil.buildSuccessResponse(isSuccess);
    }

    /**
     * 修改场景信息
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改场景信息")
    @PostMapping(value = "edit")
    public BaseResponse<Boolean> edit(@RequestBody SaveSceneRequestDto dto){
        if (
                Objects.isNull(dto)
                // 编辑时id不能为空
                || Objects.isNull(dto.getId())
                // 评估维度不能为空
                || CollectionUtils.isEmpty(dto.getDimensions())
                // 问答对不能为空
                || CollectionUtils.isEmpty(dto.getQaList())
                // 名字不能为空
                || StringUtils.isEmpty(dto.getName())
                // 图标不能为空
                || StringUtils.isEmpty(dto.getIcon())
                // 描述不能为空
                || StringUtils.isEmpty(dto.getSceneDescribe())
                // 修改场景时给需要检查下被修改场景存在
                || Objects.isNull(sceneBiz.exists(dto.getId()))

        ){
            return BaseResponseUtil.buildFailedResponse(ErrorConstants.INVALID_PARAMS);
        }
        CurrentUser user = UserHolder.getUser();
        String moduleId = user.getModuleId();
        String userId = user.getUserId();
        // 保存基础场景信息
        boolean isSuccess = sceneBiz.updateById(moduleId,userId,dto);
        return BaseResponseUtil.buildSuccessResponse(isSuccess);
    }


    /**
     * 读取场景详情
     * @param id
     * @return
     */
    @ApiOperation(value = "读取场景详情")
    @GetMapping(value = "detail")
    public BaseResponse<SceneDetailResponse> detail(@RequestParam("id") Long id){

        if (Objects.isNull(sceneBiz.exists(id))){
            return BaseResponseUtil.buildFailedResponse(ErrorConstants.INVALID_PARAMS);
        }

        CurrentUser user = UserHolder.getUser();
        String moduleId = user.getModuleId();
        // 查询所有的问答对
        SceneDetailResponse detail = sceneBiz.listDetailBySceneId(moduleId, id);

        return BaseResponseUtil.buildSuccessResponse(detail);
    }

}
